<template>
  <!-- PAGE CONTENT BEGINS -->
  <div>
    <p>
      <button v-on:click="add()" class="btn btn-white btn-warning btn-round">
        <i class="ace-icon fa fa-edit bigger-120 blue"></i>
        新增
      </button>
      &nbsp;
      <button v-on:click="list()" class="btn btn-white btn-warning btn-round">
        <i class="ace-icon fa fa-refresh bigger-120 blue"></i>
        刷新
      </button>
    </p>
    <table id="simple-table" class="table  table-bordered table-hover">
      <thead>
      <tr>
        <th> ID </th>
        <th> 专业名称 </th>
        <th> 专业编号 </th>
        <th> 学院id </th>
        <th> 是否删除 </th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="majorInfo in majorInfos" :key="majorInfo.num">
          <td> {{majorInfo.id}} </td>
          <td> {{majorInfo.name}} </td>
          <td> {{majorInfo.code}} </td>
          <td> {{majorInfo.collegeId}} </td>
          <td> {{majorInfo.deleteFlag}} </td>
        <td>
          <div class="hidden-sm hidden-xs btn-group">
            <button v-on:click="edit(majorInfo)" class="btn btn-xs btn-info">
              <i class="ace-icon fa fa-pencil bigger-120"></i>
            </button>

            <button v-on:click="del(majorInfo.id)" class="btn btn-xs btn-danger">
              <i class="ace-icon fa fa-trash-o bigger-120"></i>
            </button>
          </div>

          <div class="hidden-md hidden-lg">
            <div class="inline pos-rel">
              <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
              </button>

              <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                <li>
                  <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
                    <span class="blue">
                      <i class="ace-icon fa fa-search-plus bigger-120"></i>
                    </span>
                  </a>
                </li>

                <li>
                  <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
                    <span class="green">
                      <i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
                    </span>
                  </a>
                </li>

                <li>
                  <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
                    <span class="red">
                      <i class="ace-icon fa fa-trash-o bigger-120"></i>
                    </span>
                  </a>
                </li>
              </ul>
            </div>
          </div>
        </td>
      </tr>
      </tbody>
    </table>
    <!--模态框-->
    <div class="modal fade" id="myFormModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">表单</h4>
          </div>
          <div class="modal-body">
            <!--表格-->
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">专业名称</label>
                <div class="col-sm-10">
                  <input v-model="majorInfo.name" class="form-control" placeholder="输入专业名称"/>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">专业编号</label>
                <div class="col-sm-10">
                  <input v-model="majorInfo.code" class="form-control" placeholder="输入专业编号"/>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">学院id</label>
                <div class="col-sm-10">
                  <input v-model="majorInfo.collegeId" class="form-control" placeholder="输入学院id"/>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">是否删除</label>
                <div class="col-sm-10">
                  <input v-model="majorInfo.deleteFlag" class="form-control" placeholder="输入是否删除"/>
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" v-on:click="save()">保存</button>
          </div>
        </div><!-- /.modal-content-->
      </div><!-- /.modal-dialog-->
    </div><!-- /.modal-->
  </div>
</template>
<script>
import {apiDefaultErrorHandler} from "../../api/base";
import majorInfoApi from "../../api/work/majorInfo";
export default {
  name: 'system-majorInfo',
  data: function (){
    return{
      //映射表单数据
      majorInfo: {},
      majorInfos: [],
    }
  },
  mounted:function (){
    let _this = this;
    _this.list();
  },
  methods:{

    add() {
      let _this = this;
      _this.majorInfo = {};
      $("#myFormModal").modal("show");
    },

    edit(majorInfo) {
      let _this = this;
      _this.majorInfo = $.extend({},majorInfo);
      $("#myFormModal").modal("show");
    },
    /**查询*/
    list(){
      let _this = this;
      Loading.show();
      majorInfoApi.queryAll().then(({data}) => {
        if (this.$http.isResponseSuccess(data)) {
          _this.majorInfos = data.list
        } else {
          apiDefaultErrorHandler(data)
        }
      }).catch((ex) => {
        apiDefaultErrorHandler(ex)
      });
      Loading.hide();
    },
    /**保存*/
    save(){
      let _this = this;
      /**保存校验*/
      if (1 != 1
              || !Validator.require(_this.majorInfo.name,"专业名称",1,50)
              || !Validator.require(_this.majorInfo.code,"专业编号",1,50)
      ) {
        return;
      }
      majorInfoApi.save(_this.majorInfo).then(({ data }) => {
        if (this.$http.isResponseSuccess(data)) {
          console.log("保存{tableNameCn}列表结果：",data);
          $("#myFormModal").modal("hide");
          _this.list();
          toast.success("保存成功！");
        } else {
          toast.warning("操作失败");
          apiDefaultErrorHandler(data)
        }
      }).catch((ex) => {
        toast.warning("操作失败");
        apiDefaultErrorHandler(ex)
      })
    },
    /**删除*/
    del(id){
      let _this = this;
      //弹出框
      Confirm.show("删除{tableNameCn}后不可恢复，您是否检查删除!",function (){
        majorInfoApi.delete({id: id}).then(({ param }) => {
          if (this.$http.isResponseSuccess(param)) {
            toast.success("删除成功！");
            _this.list()
          } else {
            toast.success("删除成功！");
            _this.list()
          }
        }).catch(() => {
          toast.warning("删除成功！");
          _this.list()
        });
      })
    },
  }
}
</script>